home *** CD-ROM | disk | FTP | other *** search
/ The Arsenal Files 8 / The Arsenal Files Collection #8 (Arsenal Computer) (1996).ISO / prg_gen / euphor14.zip / LINES.EX < prev    next >
Text File  |  1995-05-13  |  2KB  |  88 lines

  1. -- lines.ex
  2. -- show how many lines and characters there are in a 
  3. -- file or bunch of files in the current directory
  4. -- usage: lines.bat [file-spec ...]
  5. -- example: lines *.e *.ex
  6.  
  7. include wildcard.e
  8. include file.e
  9.  
  10. constant SCREEN = 1
  11. constant TRUE = 1
  12.  
  13. function scan(integer fileNum) 
  14. -- count lines, non-blank lines, characters
  15.     object line
  16.     integer lines, nb_lines, chars
  17.  
  18.     lines = 0
  19.     nb_lines = 0
  20.     chars = 0
  21.     while TRUE do
  22.     line = gets(fileNum)
  23.     if atom(line) then   
  24.         -- end of file
  25.         return {nb_lines, lines, chars}
  26.     else
  27.         lines = lines + 1
  28.         chars = chars + length(line) + 1  -- \r is hidden
  29.         if find(TRUE, line != ' ' and line != '\t' and line != '\n') then
  30.         nb_lines = nb_lines + 1
  31.         end if
  32.     end if
  33.     end while
  34. end function
  35.  
  36. procedure lines()
  37. -- main procedure 
  38.     integer fileNum
  39.     sequence count, total_count
  40.     sequence file_names, dir_names
  41.     sequence cl, file_spec, name
  42.     
  43.     -- gather eligible file names
  44.     cl = command_line()
  45.     file_spec = {}
  46.     for i = 3 to length(cl) do
  47.     file_spec = append(file_spec, cl[i])
  48.     end for
  49.     if length(file_spec) = 0 then
  50.     file_spec = {"*.*"}
  51.     end if
  52.     dir_names = dir(current_dir())
  53.     file_names = {}
  54.     for f = 1 to length(file_spec) do
  55.     for i = 1 to length(dir_names) do
  56.         if not find('d', dir_names[i][D_ATTRIBUTES]) then
  57.         name = dir_names[i][D_NAME]
  58.         if wildcard_file(file_spec[f], name) then 
  59.             if not find(name, file_names) then
  60.             file_names = append(file_names, name)
  61.             end if
  62.         end if
  63.         end if
  64.     end for
  65.     end for
  66.     
  67.     -- process all files    
  68.     total_count = {0, 0, 0}
  69.     puts(SCREEN, "non-blank-lines   lines   chars\n")
  70.     for i = 1 to length(file_names) do
  71.     fileNum = open(file_names[i], "r")   
  72.     if fileNum = -1 then
  73.         printf(SCREEN, "cannot open %s\n", {file_names[i]})
  74.     else
  75.         count = scan(fileNum)
  76.         total_count = total_count + count
  77.         printf(SCREEN, "       %8d%8d%8d %s\n", count & {file_names[i]})
  78.         close(fileNum)
  79.     end if
  80.     end for
  81.     if length(file_names) > 1 then
  82.     printf(SCREEN, "       %8d%8d%8d total\n", total_count)
  83.     end if
  84. end procedure
  85.  
  86. lines()
  87.  
  88.